﻿using System.Windows;
using System.Windows.Media.Animation;

namespace ScrumTable.UI.View.Controls
{
	/// <summary>
	/// Interaction logic for WaitControl.xaml
	/// </summary>
	public partial class WaitControl 
	{
        public WaitControl()
        {
            DataContext = this;
            Loaded += OnLoaded;
            this.IsVisibleChanged += new System.Windows.DependencyPropertyChangedEventHandler(OnVisibleChanged);
        }

      

      


	    public void Pause()
	    {
            if (Resources != null && Resources.Contains("Rotate"))
            {
                ((Storyboard) this.Resources["Rotate"]).Pause();
            }
	    }

        public void Resume()
        {
            if (Resources != null && Resources.Contains("Rotate"))
            {
                ((Storyboard) this.Resources["Rotate"]).Resume();
            }
        }

        private void OnLoaded(object sender, System.Windows.RoutedEventArgs e)
        {
            InitializeComponent();
            Loaded -= OnLoaded;
            GridRectangles.Width = ActualWidth;
            GridRectangles.Height =ActualHeight;
            StartStopAnimation(IsVisible, true);
        }


        void OnVisibleChanged(object sender, System.Windows.DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                StartStopAnimation((bool)e.NewValue);
            }
            else
            {
                StartStopAnimation(false);
            }
        }

	    private void StartStopAnimation(bool isVisible, bool isLoaded=false)
	    {
            if (isLoaded || IsLoaded)
            {
                var storyBoard = ((Storyboard) Resources["Rotate"]);
                if(storyBoard != null)
                {
                    if (isVisible)
                    {
                        storyBoard.Begin();
                    }
                    else
                    {
                        storyBoard.Stop();
                    }
                }
            }
	    }
	}
}